import axios from 'axios'

let http = axios.create({
    timeout:5000
})

http.interceptors.request.use((config)=>{
    let token = sessionStorage.getItem('token')||''
    if(token){
        config.headers['x-token'] = token
    }
    return config
})

http.interceptors.response.use((response)=>{
    // response里面拿到的内容就是后台返回的内容
    console.log(response,'response的数据');
    return response.data.result
},(error)=>{
    console.log(error.response,'错误请求返回的内容');
    // return Promise.reject(error.response.data.msg)
    return Promise.reject(
        (function (resp) {

            console.group(`%c【代码${resp.status}: ${resp.data.msg}】`, 'color:blue;font-size:15px;');
            let inputdata = resp.config.data ? JSON.parse(resp.config.data) : undefined;
            let inputparams = resp.config.params ? resp.config.params : undefined;
            console.log(
                `%cPREVIEW:\n`, 'color:green;font-weight:bold;font-size:18px',
                `Data:`, inputdata, `\n`,
                `Params:`, inputparams, `\n`,
                { Method: resp.config.method }, `\n`,
                { Url: resp.config.url }, `\n`,
                { Envir: process.env.NODE_ENV }, `\n`,
                { Type: resp.config.headers['Content-Type'] }, `\n`,
                { msg: resp.data.msg }, `\n`,
                { status: resp.status }, `\n`,
            );
            console.groupEnd();
        })(error.response)
    )
})

export default http;